import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  getToken
} from "@/utils/token";

/**
 * 保持状态
 * 登录成功 => 本地存储用户信息
 * token过期则清除本地用户信息
 */


const login = () => import( /* webpackChunkName: "login_home_welome" */ '../views/login')
const home = () => import( /* webpackChunkName: "login_home_welome" */ '../views/home')
const welcome = () => import( /* webpackChunkName: "login_home_welome" */ '../views/welcome')
const send = () => import( /* webpackChunkName: "login_home_welome" */ '../views/send')
const user = () => import( /* webpackChunkName: "login_home_welome" */ '../views/user')
const page = () => import( /* webpackChunkName: "login_home_welome" */ '../views/page')
const article = () => import( /* webpackChunkName: "login_home_welome" */ '../views/article')
const classification = () => import( /* webpackChunkName: "login_home_welome" */ '../views/classification')
const comment = () => import( /* webpackChunkName: "login_home_welome" */ '../views/comment')
const nav = () => import( /* webpackChunkName: "login_home_welome" */ '../views/nav')
const system = () => import( /* webpackChunkName: "login_home_welome" */ '../views/system')
const setpwd = () => import( /* webpackChunkName: "login_home_welome" */ '../views/setpwd')

Vue.use(VueRouter)

//设计分析
/**
 * 欢迎页面 welcome
 * 发布博客 send
 * 
 * 登录页  login
 * 用户管理 user
 * 页面管理 page
 * 文章管理 article
 * 分类管理 classification
 * 评论管理 comment
 * 导航管理 nav
 * 
 * 系统配置 system
 * 修改密码 setpwd
 */
const routes = [{
    path: "*",
    redirect: "/login", //地址重定向到/home
  }, {
    path: "/", //访问更目录
    redirect: "/login", //地址重定向到/home
  },
  {
    path: "/login",
    component: login,
  },
  {
    path: "/home",
    component: home,
    meta: {
      name: '首页'
    },
    redirect: '/welcome',
    children: [{
        path: "/welcome",
        meta: {
          name: '指示板'
        },
        component: welcome,
      },
      {
        path: "/send",
        meta: {
          name: '编辑文章'
        },
        component: send,
      },
      {
        path: "/user",
        meta: {
          name: '用户管理'
        },
        component: user,
      },
      {
        path: "/page",
        meta: {
          name: '页面管理'
        },
        component: page,
      },
      {
        path: "/article",
        meta: {
          name: '文章管理'
        },
        component: article,
      },
      {
        path: "/classification",
        meta: {
          name: '分类管理'
        },
        component: classification,
      },
      {
        path: "/comment",
        meta: {
          name: '评论管理'
        },
        component: comment,
      },
      {
        path: "/nav",
        meta: {
          name: '导航管理'
        },
        component: nav,
      },
      {
        path: "/system",
        meta: {
          name: '系统配置'
        },
        component: system,
      },
      {
        path: "/setpwd",
        meta: {
          name: '修改密码'
        },
        component: setpwd,
      }
    ],
  },
];

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  //基路径 /
  base: process.env.BASE_URL,
  // base: `${process.env.BASE_API}`,
  scrollBehavior: () => ({
    y: 0
  }),
  routes
})


router.beforeEach((to, from, next) => {
  //跳转前 =》 是去login =》 有状态 =》携带from 去home
  // console.log(router.currentRoute);

  // console.log(`从${from.path}到${to.path}`);

  if (getToken() && Vue.prototype.$cache.local.getJSON('userinfo')) {
    if (to.path !== '/login') {
      next();
    } else {
      next(`/home`);
    }
  } else {
    if (to.path !== '/login') {
      Vue.prototype.$message.error('请先登录');
      next(`/login?path=${to.path}`);
    } else {
      next();
    }
  }
})

// 防止连续点击多次路由报错
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}


export default router